#img_sobel2 = cv2.Sobel(img,cv2.CV_8U,1,1,ksize=5)
image = cv2.resize(img1,(800,800))
# Kernel de convolución personalizado
# Roberts edge operator
print()
kernel_Roberts_x = np.array([
[1, 0],
[0, -1]
])
kernel_Roberts_y = np.array([
[0, -1],
[1, 0]
])
# Operador de borde Sobel
kernel_Sobel_x = np.array([
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
kernel_Sobel_y = np.array([
[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
# Operador de borde de precontrol
kernel_Prewitt_x = np.array([
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1]])
kernel_Prewitt_y = np.array([
[1, 1, 1],
[0, 0, 0],
[-1, -1, -1]])
# Operador de detección de bordes de Kirsch
def kirsch(image):
m,n = image.shape
list=[]
kirsch = np.zeros((m,n))
for i in range(2,m-1):
for j in range(2,n-1):
d1 = np.square(5 * image[i - 1, j - 1] + 5 * image[i - 1, j] + 5 * image[i - 1, j + 1] -
3 * image[i, j - 1] - 3 * image[i, j + 1] - 3 * image[i + 1, j - 1] -
3 * image[i + 1, j] - 3 * image[i + 1, j + 1])
d2 = np.square((-3) * image[i - 1, j - 1] + 5 * image[i - 1, j] + 5 * image[i - 1, j + 1] -
3 * image[i, j - 1] + 5 * image[i, j + 1] - 3 * image[i + 1, j - 1] -
3 * image[i + 1, j] - 3 * image[i + 1, j + 1])
d3 = np.square((-3) * image[i - 1, j - 1] - 3 * image[i - 1, j] + 5 * image[i - 1, j + 1] -
3 * image[i, j - 1] + 5 * image[i, j + 1] - 3 * image[i + 1, j - 1] -
3 * image[i + 1, j] + 5 * image[i + 1, j + 1])
d4 = np.square((-3) * image[i - 1, j - 1] - 3 * image[i - 1, j] - 3 * image[i - 1, j + 1] -
3 * image[i, j - 1] + 5 * image[i, j + 1] - 3 * image[i + 1, j - 1] +
5 * image[i + 1, j] + 5 * image[i + 1, j + 1])
d5 = np.square((-3) * image[i - 1, j - 1] - 3 * image[i - 1, j] - 3 * image[i - 1, j + 1] - 3
* image[i, j - 1] - 3 * image[i, j + 1] + 5 * image[i + 1, j - 1] +
5 * image[i + 1, j] + 5 * image[i + 1, j + 1])
d6 = np.square((-3) * image[i - 1, j - 1] - 3 * image[i - 1, j] - 3 * image[i - 1, j + 1] +
5 * image[i, j - 1] - 3 * image[i, j + 1] + 5 * image[i + 1, j - 1] +
5 * image[i + 1, j] - 3 * image[i + 1, j + 1])
d7 = np.square(5 * image[i - 1, j - 1] - 3 * image[i - 1, j] - 3 * image[i - 1, j + 1] +
5 * image[i, j - 1] - 3 * image[i, j + 1] + 5 * image[i + 1, j - 1] -
3 * image[i + 1, j] - 3 * image[i + 1, j + 1])
d8 = np.square(5 * image[i - 1, j - 1] + 5 * image[i - 1, j] - 3 * image[i - 1, j + 1] +
5 * image[i, j - 1] - 3 * image[i, j + 1] - 3 * image[i + 1, j - 1] -
3 * image[i + 1, j] - 3 * image[i + 1, j + 1])
# El primer método: tome el valor máximo en todas las direcciones, el efecto no es bueno, use otro método
list=[d1, d2, d3, d4, d5, d6, d7, d8]
#kirsch[i,j]= int(np.sqrt(max(list)))
# El segundo método: redondear el módulo en todas las direcciones
kirsch[i, j] =int(np.sqrt(d1+d2+d3+d4+d5+d6+d7+d8))
for i in range(m):
for j in range(n):
if kirsch[i,j]>127:
kirsch[i,j]=255
else:
kirsch[i,j]=0
return kirsch
# Detección de bordes canny k es el tamaño del núcleo gaussiano, t1, t2 son el tamaño del umbral
def Canny(image,k,t1,t2):
img = cv2.GaussianBlur(image, (k, k), 0)
canny = cv2.Canny(img, t1, t2)
return canny
# Kernel de convolución de Laplace
kernel_Laplacian_1 = np.array([
[0, 1, 0],
[1, -4, 1],
[0, 1, 0]])
kernel_Laplacian_2 = np.array([
[1, 1, 1],
[1, -8, 1],
[1, 1, 1]])
# Los siguientes dos núcleos de convolución no tienen invariancia de rotación
kernel_Laplacian_3 = np.array([
[2, -1, 2],
[-1, -4, -1],
[2, 1, 2]])
kernel_Laplacian_4 = np.array([
[-1, 2, -1],
[2, -4, 2],
[-1, 2, -1]])
# 5 * 5 plantilla de convolución LoG
kernel_LoG = np.array([
[0, 0, -1, 0, 0],
[0, -1, -2, -1, 0],
[-1, -2, 16, -2, -1],
[0, -1, -2, -1, 0],
[0, 0, -1, 0, 0]])
# Convolución
output_11 = cv2.filter2D(image, -1, kernel_Roberts_x)
output_12 = cv2.filter2D(image, -1, kernel_Roberts_y)
output_21 = cv2.filter2D(image, -1, kernel_Sobel_x)
output_22 = cv2.filter2D(image, -1, kernel_Sobel_y)
img_sobelx = cv2.Sobel(img,cv2.CV_16S,1,0)
img_sobely = cv2.Sobel(img,cv2.CV_16S,0,1)
img_sobel = img_sobelx + img_sobely
output_31 = cv2.filter2D(image, -1, kernel_Prewitt_x)
output_32 = cv2.filter2D(image, -1, kernel_Prewitt_y)
output_41 = cv2.filter2D(image, -1, kernel_Laplacian_1)
output_42 = cv2.filter2D(image, -1, kernel_Laplacian_2)
output_43 = cv2.filter2D(image, -1, kernel_Laplacian_3)
output_5 = Canny(image,3,50,150)
output_6 = kirsch(image)
# Mostrar efecto de afilado
image = cv2.resize(image, (800, 600))
output_11 = cv2.resize(output_11, (800, 600))
output_12 = cv2.resize(output_12, (800, 600))
output_13 = output_12 + output_11
output_21 = cv2.resize(output_21, (800, 600))
output_22 = cv2.resize(output_22, (800, 600))
output_23 = output_21 + output_22
output_31 = cv2.resize(output_31, (800, 600))
output_32 = cv2.resize(output_32, (800, 600))
output_33 = output_31 + output_32
output_41 = cv2.resize(output_41, (800, 600))
output_42 = cv2.resize(output_42, (800, 600))
output_43 = cv2.resize(output_42, (800, 600))
output_5 = cv2.resize(output_5, (800, 600))
output_6 = cv2.resize(output_6, (800, 600))
plt.figure(2);
plt.subplot(1,3,1),plt.imshow(output_11,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Roberts x')
plt.subplot(1,3,2),plt.imshow(output_12,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Roberts y')
plt.subplot(1,3,3),plt.imshow(output_13,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Roberts')
plt.subplots_adjust(top=4, bottom=0.1, left=0.10, right=3, hspace=0.1, wspace=0.2)
plt.figure(3);
plt.subplot(1,3,1),plt.imshow(output_21,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Sobel x')
plt.subplot(1,3,2),plt.imshow(output_22,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Sobel y')
plt.subplot(1,3,3),plt.imshow(output_23,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Sobel')
plt.subplots_adjust(top=4, bottom=0.1, left=0.10, right=3, hspace=0.1, wspace=0.2)
plt.figure(4);
plt.subplot(1,3,1),plt.imshow(img_sobelx,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Sobel x')
plt.subplot(1,3,2),plt.imshow(img_sobely,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Sobel y')
plt.subplot(1,3,3),plt.imshow(img_sobel,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Sobel')
plt.subplots_adjust(top=4, bottom=0.1, left=0.10, right=3, hspace=0.1, wspace=0.2)
plt.figure(5);
plt.subplot(1,3,1),plt.imshow(output_31,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Prewitt x')
plt.subplot(1,3,2),plt.imshow(output_32,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Prewitt y')
plt.subplot(1,3,3),plt.imshow(output_33,'gray'),plt.xticks([]), plt.yticks([]),plt.title('Prewitt')
plt.subplots_adjust(top=4, bottom=0.1, left=0.10, right=3, hspace=0.1, wspace=0.2)